<?php
include_once 'config.php';
include_once 'file_manager.php';

$file = File_Manager::getInstance();

$curl = curl_init(URL_CONTACTS);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_FAILONERROR, true);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_HTTPHEADER, array("Authorization: GoogleLogin auth=" . $file->getToken(), "GData-Version:3.0"));

if(curl_exec($curl) === false)
{
    echo 'Curl error: ' . curl_error($curl);
	die();
}

$response = curl_multi_getcontent($curl);
curl_close($curl);

/*echo header ( "Content-type: text/xml; charset=utf-8" );
echo $response;
die;*/

//The following is an example of a contacts feed with only one entry.
/**
<feed xmlns='http://www.w3.org/2005/Atom'
    xmlns:openSearch='http://a9.com/-/spec/opensearch/1.1/'
    xmlns:gContact='http://schemas.google.com/contact/2008'
    xmlns:batch='http://schemas.google.com/gdata/batch'
    xmlns:gd='http://schemas.google.com/g/2005'
    gd:etag='W/"CUMBRHo_fip7ImA9WxRbGU0."'>
  <id>liz@gmail.com</id>
  <updated>2008-12-10T10:04:15.446Z</updated>
  <category scheme='http://schemas.google.com/g/2005#kind' term='http://schemas.google.com/contact/2008#contact' />
  <title>Elizabeth Bennet's Contacts</title>
  <link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml'
    href='https://www.google.com/m8/feeds/contacts/liz%40gmail.com/full' />
  <link rel='http://schemas.google.com/g/2005#post' type='application/atom+xml'
    href='https://www.google.com/m8/feeds/contacts/liz%40gmail.com/full' />
  <link rel='http://schemas.google.com/g/2005#batch' type='application/atom+xml'
    href='https://www.google.com/m8/feeds/contacts/liz%40gmail.com/full/batch' />
  <link rel='self' type='application/atom+xml' href='https://www.google.com/m8/feeds/contacts/liz%40gmail.com/full?max-results=25' />
  <author>
    <name>Elizabeth Bennet</name>
    <email>liz@gmail.com</email>
  </author>
  <generator version='1.0' uri='http://www.google.com/m8/feeds'>
    Contacts
  </generator>
  <openSearch:totalResults>1</openSearch:totalResults>
  <openSearch:startIndex>1</openSearch:startIndex>
  <openSearch:itemsPerPage>25</openSearch:itemsPerPage>
  <entry gd:etag='"Qn04eTVSLyp7ImA9WxRbGEUORAQ."'>
    <id>
      http://www.google.com/m8/feeds/contacts/liz%40gmail.com/base/c9012de
    </id>
    <updated>2008-12-10T04:45:03.331Z</updated>
    <app:edited xmlns:app='http://www.w3.org/2007/app'>2008-12-10T04:45:03.331Z</app:edited>
    <category scheme='http://schemas.google.com/g/2005#kind'
      term='http://schemas.google.com/contact/2008#contact' />
    <title>Fitzwilliam Darcy</title>
    <gd:name>
      <gd:fullName>Fitzwilliam Darcy</gd:fullName>
    </gd:name>
    <link rel='http://schemas.google.com/contacts/2008/rel#photo' type='image/*'
      href='https://www.google.com/m8/feeds/photos/media/liz%40gmail.com/c9012de'
      gd:etag='"KTlcZWs1bCp7ImBBPV43VUV4LXEZCXERZAc."' />
    <link rel='self' type='application/atom+xml' 
    	href='https://www.google.com/m8/feeds/contacts/liz%40gmail.com/full/c9012de' />
    <link rel='edit' type='application/atom+xml' 
    	href='https://www.google.com/m8/feeds/contacts/liz%40gmail.com/full/c9012de' />
    <gd:phoneNumber rel='http://schemas.google.com/g/2005#home' primary='true'>
      456
    </gd:phoneNumber>
    <gd:extendedProperty name='pet' value='hamster' />
    <gContact:groupMembershipInfo deleted='false' href='http://www.google.com/m8/feeds/groups/liz%40gmail.com/base/270f' />
  </entry>
</feed>
 */

include_once 'Contacto.php';

if ($response) {
	$xmlContacts = new SimpleXMLElement($response);

	$lastUpdate 	= (string)$xmlContacts->updated;
	$owner 			= (string)$xmlContacts->author->name;
	$emailOwner 	= (string)$xmlContacts->author->email;
	
	$contactos = array();
	
	foreach ($xmlContacts->entry as $contactEntry) {
		
		$last = (string)$contactEntry->updated;
		$nombre = (string)$contactEntry->title;
		
		$photo = "";
		//buscar el link de la foto
		foreach ($contactEntry->link as $link) {
			if ((string)$link->attributes()->rel == "http://schemas.google.com/contacts/2008/rel#photo") {
				$photo = (string)$link->attributes()->href;
			}
		}
		
		//obtener los nodos sin el schema Ej: <gd:email ...
		$defaults = $contactEntry->children(URL_SCHEMA);
		
		$emails = array();
		foreach ($defaults->email as $dirEmail) {
			$emails[] = (string)$dirEmail->attributes()->address;
		}
		
		$contactos [] = new Contacto($last, $nombre, $emails, $photo);
	}
}

echo "Propietario: ".$owner."<br/>";
echo "e-mail: ".$emailOwner."<br/>";

foreach ($contactos as $contact) {
	echo "<span>".$contact->Nombre()."</span><br/>";
	echo "<img alt='".$contact->Nombre()."' src='photo.php?url=".$contact->photo()."' /><br/>";
	echo "<br/>";
}

echo "<br/><a href='../index.php'>Inicio</a><br/>";
echo "<a href='logout.php'>Salir</a>";

?>
